﻿package com.dg.gaming.api.common{
	import com.dg.gaming.api.common.msg.ITransmittable;
	
	import flash.events.EventDispatcher;
	
	import nanoxml.XMLElement;

	/**
	 * ...
	 * @author ...
	 */
	public class AbstractConnectionThread extends EventDispatcher{

		/** All clients start initially with "connected" equal to false
		 *  (although they can still recieve/transfer logon information). */
		protected var connected:Boolean=false;
		/** Username of the client. */
		protected var userid:Number;
		/** When the boolean loop becomes false the Thread finishes. */
		protected var loop:Boolean=true;
		/**
		 * This abstract method must be overwritten by a child which extends this
		 * class.
		 *
		 * @param message       Communication as an XML object.
		 * @throws TransmissionException  This is thrown if there is a problem parsing the String.
		 */
		public function parse(message:XMLElement):void {

		}

		/**
		 * This method is called to properly clean up after a client.
		 */
		public function cleanup():void {
		}

		public function AbstractConnectionThread() {
		}


		/**
		 * Stop the loop.
		 */
		public function stopLoop():void {
			this.loop=false;
		}

		/**
		 * Set boolean to specify that this client has connected sucessfully.
		 */
		public function connect():void {
			this.connected=true;
		}

		/**
		 * Send a ITransmittable object to the output stream (could be server or
		 * client).
		 *
		 * @param transObject
		 */
		public function send(transObject:ITransmittable):void {
		}

		//    {
		//        // Retrieve XMLElement from the object and flatten to a String.
		//        String message = transObject.flatten().toString();
		//
		//        // Send down the socket to the receiving end
		//        out.println(message);
		//    }

		/**
		 * Returns the username.
		 *
		 * @return  Username of client / server who created this thread.
		 */
		public function getUserId():Number {
			return userid;
		}

		/**
		 * Set the username.
		 *
		 * @param username  Username of client / server who created this thread.
		 */
		public function setUsername(userid:Number):void {
			this.userid=userid;
		}


	}

}